package morphage.engine;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import java.util.Vector;
import java.util.Collection;

public class Pipeline
{
  private Vector<OperationLayer> m_layers = new Vector<OperationLayer>();
  private static Pipeline        s_defaultPipeline = new Pipeline();
 
  public Pipeline()
  {
    m_layers.add( new HarrisDetector_CornerResponseLayer() );
    m_layers.add( new ThresholdLayer() );
    m_layers.add( new HarrisDetector_MaximaLayer() );
  }
 
  public synchronized Image Compute(BufferedImage firstImage, BufferedImage lastImage)
  {
    assert( firstImage != null );
    assert( lastImage != null );
    
    LayerData           layer   = new BaseOperationLayer();
    MatrixBufferedImage img     = new MatrixBufferedImage( firstImage );
    img.image2Matrix();
    layer.setImage( img );
    layer.setFirstImage( firstImage );
    layer.setLastImage( lastImage );
    
    Iterator<OperationLayer> it = m_layers.iterator();
    while( it.hasNext() )
    {
      OperationLayer op = it.next();
      layer             = op.Compute( layer );
    }
    layer.m_image.matrix2Image();
    return layer.getImage();
  }
  
  public Collection<OperationLayer> getOperationLayers()
  {
    return m_layers;
  }
  
  public OperationLayer getOperationLayer(int id)
  {
    assert( id >= 0 );
    assert( id < m_layers.size() );
    
    return m_layers.get( id );
  }
  
  public synchronized LayerData getIntermediateResult(int operationId)
  {
    assert( operationId >= 0 );
    assert( operationId < m_layers.size() );
    
    return m_layers.get( operationId ).getData();
  }
  
  public static Pipeline getDefaultPipeline()
  {
    return s_defaultPipeline;
  }
  
  private class BaseOperationLayer extends LayerData
  {
    
  }
}
